import { _decorator, Component, Label, macro, Node, Animation, Color } from 'cc';
import { GameMainController } from './GameMainController';
import { Utils } from './Utils';
const { ccclass, property } = _decorator;

@ccclass('TimerScript')
export class TimerScript extends Component {

    @property(Label)
    timerLabel: Label | null = null; // 引用场景中的Label组件

    @property(Node)
    animLable: Node | null = null; // 动画场景
    @property(Node)
    bg: Node | null = null; // 动画场景

    private countdown: number = 3; // 倒计时初始值，可以根据需要调整
    private timerRunning: boolean = false;
    private pause: boolean = false

    private gameController: GameMainController
    private playAlarm : boolean = false

    public setController(controller: GameMainController) {
        this.gameController = controller
    }

    start() {
    }

    update(deltaTime: number) {

    }

    startCountdown() {
        if (this.timerRunning) {
            return; // 如果计时器已经在运行，就不再重复启动
        }
        this.playAlarm = false;
        this.countdown = 120
        this.timerRunning = true;
        this.timerLabel.color = Utils.inst.hexToCcColor("#242421")
        this.updateTimerLabel();
        this.schedule(this.tick, 1, macro.REPEAT_FOREVER);
    }

    stopCountdown() {
        this.unschedule(this.tick);
        this.timerRunning = false;
    }

    // 每秒调用的函数
    private tick() {
        if (this.pause) {
            return
        }
        if (this.countdown > 0) {
            this.countdown--;
            this.updateTimerLabel();
        } else {
            this.stopCountdown();
            // 可以在这里添加倒计时结束时的逻辑
            console.debug("倒计时完成！");
            if (this.gameController) {
                this.stopCountdown()
                this.gameController.gameOver();
            }
        }
    }

    public setPause(pause: boolean) {
        this.pause = pause;
    }

    // 更新Label显示
    private updateTimerLabel() {
        if (this.timerLabel) {
            if(this.countdown <= 10){
                this.playAlarmAnim();
                this.timerLabel.color = Utils.inst.hexToCcColor("#EA0707")
            }
            if(this.countdown < 0){
                return;
            }
            let minute = Math.floor(this.countdown / 60)
            let second = this.countdown % 60
            let formattedSeconds = second < 10 ? '0' + second : second.toString();
            let formattedMinute = minute < 10 ? '0' + minute : minute.toString();
            this.timerLabel.string = `${formattedMinute}:${formattedSeconds}`;
        }
    }

    // 公开方法，用于动态增减时间
    public adjustTime(amount: number) {
        this.playAnim(amount)
        this.countdown += amount;
        this.updateTimerLabel();
    }

    private playAnim(amount: number) {
        this.animLable.active = true
        let anim = this.animLable.getComponent(Animation)
        let txt = this.animLable.getComponent(Label)
        anim.on(Animation.EventType.FINISHED, () => {
            this.animLable.active = false
        }, this);
        if (amount > 0) {
            txt.string = "+" + amount
            anim.play("scoreAdd")
        } else {
            txt.string = "" + amount
            anim.play("scoreReduce")
        }
    }

    private playAlarmAnim() {
        if(this.playAlarm){
            return
        }
        this.playAlarm = true;
        console.debug("playAlarmAnim")
        let anim = this.bg.getComponent(Animation)
        anim.crossFade("timerAlarm")
        this.scheduleOnce(() => {
            anim.crossFade("timerNormal")
        }, 1)
    }
}


